/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    15 Aug 2022 - 11:01:26
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/

/* Regular PMT model using all SISBEN varibles and SISBEN income
 This will serve as baseline for comparison */


 /*==================================================
               0: Program set up
 ==================================================*/
 *Written on STATA 17
 drop _all
 set varabbrev off	// no variable abbreviations allowed (personal preference)
 
 *----------  1.1. Load data 
 use "${dir3}/02_admin/SISBEN_foruse.dta", clear // Load SISBEN full data
 
 ** safe guard to drop all existing variables that should be created by this do
 foreach v in _sample l_pp_inc ihs_pp_inc cookpower_connected living_couple prop_kids {
	cap drop `v'
 }
 
 *----------  1.2. Split sample
 // random sampling
 sort id_persona
 set seed 339487731
 
 // An uniform have 50% probably of being > 0.5, let's use that for the split
 gen  _random = runiform()
 gen _sample = 1
 replace _sample = 2 if _random > 0.5
 lab var _sample "Training or testing sample"
 lab define lsmaple 1 "Training" 2 "Testing", replace
 lab values _sample lsmaple
 drop _random 
 
 *----------  1.3. Prices update 
  // most of these should be at 2017 prices, we adjust to NOV 2019 prices asumming data gathering took place on NOV 2017
 foreach v in pp_inc pp_inc_nt inc_t inc_nt_t inc_t_w inc_nt_t_w {
 	cap confirm var `v'_0
	if _rc {
		noi di as error "`v'_0 does not exist"
		gen `v'_0 = `v'
		lab var `v'_0 	"`: variable label `v'' - original values"
	}
	else {
		noi di as result "`v'_0 exists."
	}
	replace `v' = (`v'_0 * 1.0724) // deflactor IPC2019/IPC2017 at nov values
 }
 
 *----------  1.4. Income transformation (main output var)
  gen ihs_pp_inc = asinh(pp_inc_nt) // Inverse hyperbolic sine of PP income net of transfers
  gen l_pp_inc = log(pp_inc_nt) // Inverse hyperbolic sine of PP income net of transfers
  *replace l_pp_inc = 0 if pp_inc_nt == 0 // equivalent to give them one 1 COP --> 0
 *----------  1.5. PMT variables transformation
 gen cookpower_connected = (cookpower_electric == 1 | cookpower_natgas == 1)
 gen living_couple = (free_union == 1 | married == 1 | in_couple==1) 
 gen prop_kids = n_kids_18 / n_members

 *---------- Drop probelmatic observations
 drop if inlist(nivel, "A", "B", "C", "D")
 
 *---------- 1.6 Final save 
 compress 
 save "${dir3}/02_admin/SISBEN_foruse.dta", replace 

 
 
 